package com.example.notificationtest;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.send_notice:
                Intent intent = new Intent(this, NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                   String id = "myChannel";
                   String name = "我的渠道";
                   NotificationChannel channel = new NotificationChannel(id, name,NotificationManager.IMPORTANCE_DEFAULT);
                   manager.createNotificationChannel(channel);
                   notification = new NotificationCompat.Builder(this, id)
                           .setChannelId(id)
                           .setContentTitle("This is content title")
                           .setContentText("This is content text")
                           .setWhen(System.currentTimeMillis())
                           .setSmallIcon(R.mipmap.ic_launcher)
                           .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                   R.mipmap.ic_launcher))
                           .setContentIntent(pi)
                           .setAutoCancel(true)
                           .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
                           .setVibrate(new long[]{0, 1000, 1000, 1000})
                           .setLights(Color.WHITE, 1000, 1000)
                           .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(
                                   getResources(), R.drawable.big_image)))
                           .build();
                   manager.notify(1, notification);
               }else{
                   notification = new NotificationCompat.Builder(this, "default")
                           .setContentTitle("This is content title")
                           .setContentText("This is content text")
                           .setWhen(System.currentTimeMillis())
                           .setSmallIcon(R.mipmap.ic_launcher)
                           .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                   R.mipmap.ic_launcher))
                           .setContentIntent(pi)
                           .setAutoCancel(true)
                           .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
                           .setVibrate(new long[]{0, 1000, 1000, 1000})
                           .setLights(Color.WHITE, 1000, 1000)
                           .build();
                   manager.notify(1, notification);
               }

                break;
            default:
                break;
        }
    }
}
